package com.erp.web.erp.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.erp.base.business.entity.AppUser;
import com.erp.base.common.utils.AppUserUtils;
@Component
public class AppUserInterceptor implements HandlerInterceptor{
	private static Logger logger=LoggerFactory.getLogger(AppUserInterceptor.class);
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		String requestURI=request.getRequestURI();
		if(requestURI.contains("login")||requestURI.contains("register")){
			return true;
		}
		AppUser appUser =AppUserUtils.getCurrentUser(request);
		if(appUser==null){
			logger.info("preHandle：未登录，跳转到登录页面！"+"请求地址 : " + request.getRequestURL().toString());
			//request.getRequestDispatcher("/templates/erp/index/login.html").forward(request, response);
			//response.sendRedirect("/erp/index/login");
			//request.setAttribute("javax.servlet.error.status_code",445); 
			//response.setStatus(445);
			response.setCharacterEncoding("UTF-8");  
			response.setContentType("application/json; charset=utf-8");  
			response.getWriter().append("{\"code\":445,\"message\":\"未登录/登录超时，请重新登录！\"}");
			return false;
		}
		return HandlerInterceptor.super.preHandle(request, response, handler);
	}
}
